package com.parkinglot.service.Impl;

import com.parkinglot.core.Result;
import com.parkinglot.dao.MonthbilRecordDao;
import com.parkinglot.dao.OrderDao;
import com.parkinglot.dao.UserCarDao;
import com.parkinglot.dao.UserDao;
import com.parkinglot.parmeter.GarageUser;
import com.parkinglot.parmeter.HaveCarnumber;
import com.parkinglot.parmeter.UserCarMonthvip;
import com.parkinglot.parmeter.UserParm;
import com.parkinglot.pojo.MonthbilRecord;
import com.parkinglot.pojo.Order;
import com.parkinglot.pojo.User;
import com.parkinglot.pojo.UserCar;
import com.parkinglot.service.OrderService;
import com.parkinglot.service.UserService;
import com.parkinglot.util.*;
import com.parkinglot.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 15:29:55
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserCarDao userCarDao;

    @Resource
    private OrderService orderService;
    @Resource
    private HttpUrlutils httpUrlutils;
    @Resource
    private OrderDao orderDao;
    @Resource
    private MonthbilRecordDao monthbilRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UserVo queryById(String id) {
        return this.userDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param userParm 实例对象
     * @return 实例对象
     */
    @Override
    @CacheEvict(value = "listuser", allEntries = true, beforeInvocation = true)
    public int insert(UserParm userParm) {
        User u = new User();
        BeanUtils.copyProperties(userParm, u);
        return this.userDao.insert(u);
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public UserVo update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserid());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    @Cacheable(value = "listuser")
    public List<UserVo> getalluser() {
        List<UserVo> list = null;
        try {
            list = userDao.queryAllUser();
            if (list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<UserVo> userzrcount() {
        List<UserVo> list = null;
        try {
            list = userDao.userzrcount();
            if (list.size() > 0)
                return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<UserVo> userweekcount(String gardecode) {
        List<UserVo> list = null;
        try {
            list = userDao.userweekcount(gardecode);
            if (list.size() > 0)
                return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<UserVo> usermonthcount(String gardecode) {
        List<UserVo> list = null;
        try {
            list = userDao.usermonthcount(gardecode);
            if (list.size() > 0)
                return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<UserCarVo> queryUserByid(GarageUser garageUser) {
        UserVo userVo = null;
        UserCar userCar = new UserCar();
//        UserCarVo userCar1 = new UserCarVo();
        List<UserCarVo> userCar1 = new ArrayList<>();
        User user = new User();
        CarNumberInit carNumberInit = new CarNumberInit();
        try {
            userVo = userDao.queryById(garageUser.getUserid());
            if (userVo == null) {
                String carnumber = carNumberInit.carinit();
                userCar.setCarnumber(carnumber);
                userCar.setUserid(garageUser.getUserid());
                userCar.setGarageCode(garageUser.getGarageCode());
                userCar.setBindcarnumbertime(DateUtils.getFormatDate(new Date()));
                user.setUserid(garageUser.getUserid());
                user.setVipstatus(0);
                userCarDao.insert(userCar);
                userDao.insert(user);
                userCar1 = userCarDao.queryByIds(garageUser.getUserid());

            } else if (garageUser.getType().equals("1")) { //假设无牌车二维码有个状态位1 表示要随机生成车牌
                String carnumber = carNumberInit.carinit();
                userCar.setCarnumber(carnumber);
                userCar.setUserid(garageUser.getUserid());
                userCar.setGarageCode(garageUser.getGarageCode());
                userCarDao.insert(userCar);
                //userDao.insert(user);
                userCar1 = userCarDao.queryByIds(garageUser.getUserid());
            } else {
                userCar1 = userCarDao.queryByIds(garageUser.getUserid());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userCar1;
    }

    /**
     * 无牌车车辆入场时  绑定车牌并生成订单  抬杆
     *
     * @param garageUser
     * @return
     */
    @Override
    public Result BindCarNumber(GarageUser garageUser)throws Exception {
        UserCar userCar = new UserCar();

        UserCar adduserCar = new UserCar();
        UserCarVo userCarVo = null;
        OrderqueryVo orderqueryVo = null;
        Order order = new Order();
        User user = new User();
        String checkcarnumber= CheckCarnumberUtil.checkcarnumber(garageUser.getCarnumber());

        UserCarVo userCarVo1=userCarDao.getusercar(garageUser.getGarageCode(),garageUser.getUserid(),garageUser.getCarnumber());
        if(userCarVo1!=null){
            if(garageUser.getCarnumber().equals(userCarVo1.getCarnumber())){
                userCarVo1.setMess("输入的车牌号重复");
                return Result.failure("输入的车牌号重复");
            }
        }else {
            UserVo userVo = userDao.queryuserexistence(garageUser.getUserid());//查询用户是否存在
            if (userVo != null) {   //用户存在只是添加新的车 一个人对应多辆车
                BeanUtils.copyProperties(garageUser, userCar);
                userCar.setCartype(checkcarnumber);
                userCarDao.insert(userCar);
            } else {
                //用户不存在  新增
                user.setUserid(garageUser.getUserid());
                BeanUtils.copyProperties(garageUser, adduserCar);
                userDao.insert(user);
                adduserCar.setCartype(checkcarnumber);
                userCarDao.insert(adduserCar);
            }
            userCarVo = userCarDao.queryuserCar(garageUser);


            //生成订单
            order.setGarageid(garageUser.getGarageCode());
            order.setCarnumber(garageUser.getCarnumber());
            order.setUserid(garageUser.getUserid());
            order.setParkingTime(DateUtils.getFormatDate(new Date()));
            order.setOrdernumber(RandomsUtil.buildRandom());
            order.setType(0);
            orderDao.insert(order);
            orderqueryVo = orderDao.queryBycarnumber(order);
            userCarVo.setParkingTimesjc(DateUtils.getParse(orderqueryVo.getParkingTime()).getTime());
            userCarVo.setOrdernumber(orderqueryVo.getOrdernumber());


            //查询月票信息是否有该用户 并查出该用户最近购买记录
            MonthbilRecord monthbilRecord = monthbilRecordDao.queryById(garageUser.getGarageCode(), garageUser.getUserid(), garageUser.getCarnumber());
            if (monthbilRecord != null) {
                BeanUtils.copyProperties(monthbilRecord, userCarVo);

            } else {
                userCarVo.setType("0");
            }

           /*     String tgurl = "http://192.168.3.205:8080/inPark/unlicensedInPark?carNumber="
                        + garageUser.getCarnumber()
                        + "&parkId="
                        + garageUser.getGarageCode();
                Map<String, Object> map = httpUrlutils.getresult(tgurl);*/
        }


        return Result.success(userCarVo);
    }

    @Override
    public UserCarMonthVo monthBindCarNumber(GarageUser garageUser) {
        User user = new User();
        UserCar adduserCar = new UserCar();
        UserCar userCar = new UserCar();
        UserCarMonthVo userCarMonthVo = new UserCarMonthVo();
        //查询用户是否存在
        UserVo userVo = userDao.queryuserexistence(garageUser.getUserid());
        //查询用户有没有绑定车
        UserCarVo userCarVo = userCarDao.usercarextence(garageUser);
        //查询月票信息是否有该用户 并查出该用户最近购买记录
        MonthbilRecord monthbilRecord = monthbilRecordDao.queryById(garageUser.getGarageCode(), garageUser.getUserid(), garageUser.getCarnumber());
        if (userCarVo == null && userVo == null) {//用户不存在 返回界面让用户先绑定车牌
            userCarMonthVo.setType("0");
        } else if (monthbilRecord == null) {
            userCarMonthVo.setType("0");
        } else {
            userCarMonthVo.setType("1");
        }
       /* if(userVo != null && userCarVo==null){
            BeanUtils.copyProperties(garageUser, userCar);
            userCarDao.insert(userCar);
        }else {
            //用户输完车牌号 在调这个接口
            user.setUserid(garageUser.getUserid());
            BeanUtils.copyProperties(garageUser, user);
            BeanUtils.copyProperties(garageUser, adduserCar);
            userDao.insert(user);
            userCarDao.insert(adduserCar);
        }*/
       /* if(monthbilRecord==null) {
            userCarMonthVo.setType("0");
        }*/

        return userCarMonthVo;

    }

    @Override
    public UserVo queryuserexistence(String userid) {
        UserVo userVo = null;
        try {
            userVo = userDao.queryuserexistence(userid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userVo;
    }

    @Override
    public Result getusercarmonthvip(UserCarMonthvip userCarMonthvip) {

        List<MonthbilRecordVo> monthbilRecordVos = monthbilRecordDao.getusercarmonthvip(userCarMonthvip);
        return Result.success(monthbilRecordVos);
    }

    /**
     * 有牌车入场 扫了预付款二维码 中间件给的车牌号 需要用户去绑定车牌
     * 无牌车扫码就把车牌号带出去
     */
    @Override
    public Result havecarnumber(HaveCarnumber haveCarnumber) {
        UserCar userCar = new UserCar();
        UserCar adduserCar = new UserCar();
        UserCarVo userCarVo = null;
        GarageUser garageUser=new GarageUser();

        User user = new User();
        int code = 0;
        try {
                UserVo userVo = userDao.queryuserexistence(garageUser.getUserid());//查询用户是否存在
            UserCarVo userCarVo1= userCarDao.queryuserCar(garageUser);//查询用户的车是否存在
            if(null!=userCarVo1.getCarnumber()){
                return Result.success(userCarDao.queryuserCar(garageUser));
            }
                if (userVo != null) {   //用户存在只是添加新的车 一个人对应多辆车

                    BeanUtils.copyProperties(garageUser, userCar);
                    userCarDao.insert(userCar);
                } else {
                    //用户不存在  新增
                    user.setUserid(garageUser.getUserid());
                    BeanUtils.copyProperties(garageUser, adduserCar);
                    userDao.insert(user);
                    userCarDao.insert(adduserCar);
                }
            userCarVo = userCarDao.queryuserCar(garageUser);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(userCarVo);
    }

    @Override
    public UserCarVo checkcarnumber(String garageid, String userid, String carnumber) {


        return userCarDao.checkcarnumber(garageid,userid,carnumber);
    }

    @Override
    public Result getusercarpaysattus(String garageid, String userid) {
        List<UserCarBindDetilVo> userCarBindDetilVo=null;
        try{
             userCarBindDetilVo=   userDao.getusercarpaysattus(garageid,userid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(userCarBindDetilVo);
    }

}